
package controller;

import DAO.BaseDAO;
import DAO.CategoryDA;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpSession;
import model.Category;
import model.User;
import org.apache.commons.mail.EmailException;
import org.primefaces.context.RequestContext;
import validator.e;

@ManagedBean(name = "categoryController")
@SessionScoped
public class CategoryController {
    public Category category;
    public List lista;
    public DataModel listCategory; 

    public CategoryController(){
        category = new Category();
    }
    
     public Category getCategory()
    {
        return category;
    }
    
    public void setCategory(Category category)
    {
        this.category = category;
    }
    
    public void saveCategory() throws EmailException
    {
        BaseDAO dao = new CategoryDA();
        //CategoryDao dao = new CategoryDaoImp();
        dao.save(category);
    }
    
      public DataModel getListCategory()
    {
      List<Category> list = new CategoryDA().list();
      listCategory = new ListDataModel(list);
      return listCategory;
    }
      
      public String preparaAlterar()
    {
        category = (Category) (listCategory.getRowData());
        return "changeCategory";
    }
      
      public String changeCategory()
    {
        BaseDAO dao = new CategoryDA();
        dao.update(category);
        setCategory(new Category());
        return "consultCategory";
    }
      
      public String removeCategory(){

        Category categoryTemp = (Category)(listCategory.getRowData());
        BaseDAO dao = new CategoryDA();
        dao.remove(categoryTemp);
        return "#";

    }
      
      public void clearCategory()
      {
      category = new Category();
      }
}
